package com.clean.cleantotal.utils

import android.app.ActivityManager
import android.content.Context
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.io.InputStreamReader

object MyDeviceUtils {
    fun getCpuCoresCount(): Int {
        var processorCount = 0
        try {
            val process = Runtime.getRuntime().exec("cat /sys/devices/system/cpu/present")
            process.waitFor()
            val reader = BufferedReader(InputStreamReader(process.inputStream))
            var line: String
            if (reader.readLine().also { line = it } != null) {
                processorCount = line.substring(line.indexOf("-") + 1).toInt() + 1
            }
            process.destroy()
            reader.close()
        } catch (e: IOException) {
            processorCount = Runtime.getRuntime().availableProcessors()
            e.printStackTrace()
        } catch (e: InterruptedException) {
            processorCount = Runtime.getRuntime().availableProcessors()
            e.printStackTrace()
        } catch (e: NumberFormatException) {
            processorCount = Runtime.getRuntime().availableProcessors()
            e.printStackTrace()
        }
        return processorCount
    }

    fun getCpuSpeed(): String {
        val maxFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
        var maxFreq = ""
        try {
            val br = BufferedReader(FileReader(maxFreqPath))
            maxFreq = br.readLine()
            br.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return try {
            val result = maxFreq.toInt() / 1000
            "$result MHZ"
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            "1253 MHZ"
        }
    }

    fun getUsage(number: Int): String {
        var c = 0
        for (i in 0 until number) {
            c += (getCpuUsage(i) * 100).toInt()
        }
        return (c / number).toString() + "%"
    }

    private fun getCpuUsage(i: Int): Float {
        val process1: Process
        val process2: Process
        val reader1: BufferedReader
        val reader2: BufferedReader
        var freq = 0
        var maxFreq = 0
        try {
            process1 = Runtime.getRuntime()
                .exec("cat /sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq")
            reader1 = BufferedReader(InputStreamReader(process1.inputStream))
            process2 = Runtime.getRuntime()
                .exec("cat /sys/devices/system/cpu/cpu$i/cpufreq/cpuinfo_max_freq")
            reader2 = BufferedReader(InputStreamReader(process2.inputStream))
            var line1: String
            var line2: String
            if (reader1.readLine().also { line1 = it } != null) {
                freq = line1.toInt() / 1000
            }
            if (reader2.readLine().also { line2 = it } != null) {
                maxFreq = line2.toInt() / 1000
            }
            process1.destroy()
            reader1.close()
            process2.destroy()
            reader2.close()
        } catch (e: Exception) {
            e.message
            return 0.5f
        }
        return if (maxFreq == 0) 0.5f else freq.toFloat() / maxFreq
    }

    fun getBatteryTotalPower(context: Context): Int {
        val power: Any
        var capacity = 0.0
        try {
            power = Class.forName("com.android.internal.os.PowerProfile").getConstructor(
                Context::class.java
            ).newInstance(context)
            capacity = Class.forName("com.android.internal.os.PowerProfile")
                .getMethod("getBatteryCapacity").invoke(power) as Double
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return capacity.toInt()
    }

    fun getMemoryPercent(context: Context): String {
        val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        manager.getMemoryInfo(memoryInfo)
        val total = memoryInfo.totalMem
        val used = total - memoryInfo.availMem
        return "${used * 100 / total}"
    }
}